package com.apes.project.jekunAuto.timeTask;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.pi.model.Timestamp;
import com.apes.pi.repository.TimestampRepository;
import com.apes.pi.service.PiManager;
import com.apes.project.jekunAuto.report.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Service("baseService")
public class BaseService extends DomainService {

    @Autowired
    ReportService reportService;

    @Autowired
    TimestampRepository timestampRepository;

    @Autowired
    PiManager piManager;
    /*
    * 同步老系统数据到新系统
    * interfaceId 老系统接口名称
    * */
    public JSONArray getData(String interfaceId) {
        // 取接口调用的时间戳
        Timestamp timestamp = timestampRepository.findOne(interfaceId);

        Map data = new HashMap();
        if(timestamp != null) {
            data.put("TIMESTAMP",timestamp.getTime());
        }else {
            data.put("TIMESTAMP",LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE) + ".000000.000");
        }

        JSONObject resultObj = piManager.synErp(interfaceId, data);

        JSONArray resultArray = JSONArray.parseArray(resultObj.getString("obj"));

        // 更新最后的时间戳到 res_timestamp 表记录, 下次定时任务用
        if(resultArray.size() > 0) {

            // 获取最后的时间戳
            String lastTimeTamp = resultArray.getJSONObject(resultArray.size() - 1).getString("TS01");
            if(timestamp == null) {
                timestamp = new Timestamp();
            }
            timestamp.setId(interfaceId);
            timestamp.setType("timestamp");
            timestamp.setTime(lastTimeTamp);
            timestampRepository.save(timestamp);
        }
        return resultArray;
    }
    /*
     * 同步老系统数据到新系统(绩效)
     * interfaceId 老系统接口名称
     * */
    public JSONArray getData(String interfaceId,JSONObject data) throws Exception{
        data.put("interfaceMethod", interfaceId);
        data.put("pdata",data);
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(data);
        JSONArray resultArray = null;
        try {
            JSONObject resultObj = JSONObject.parseObject(reportService.interfaceData(simpleRequest));
            String obj = resultObj.getString("obj");
            JSONObject object = JSONArray.parseArray(obj).getJSONObject(0);
            JSONArray rewardPunishList = object.getJSONArray("RewardPunishList");
//            String rewardPunishList = "[{\"note\":\"车牌号码:粤AM4V87,要货最后到店日期：30-9月 -18\",\"SourceOrder\":\"粤AM4V87\",\"SourceName\":\"车牌号码\",\"employee\":[{\"qty\":\"1\",\"Id\":\"0000299\"}]}]";
            resultArray =rewardPunishList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultArray;
    }

    public Boolean setData(String interfaceId,JSONObject setJsonObject) {
        Map data = new HashMap();
        data.put("interfaceMethod", interfaceId);
        data.put("pdata", setJsonObject);
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(data);
        Boolean flag = true;
        JSONObject resultObj = JSONObject.parseObject(reportService.interfaceData(simpleRequest));
        if (!resultObj.getBoolean("success")) {
            flag =  false;
        }
        return flag;
    }

}
